﻿/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:23:01 AM
------------------------------------------------------*/
using hou_elearning.DataAccess.Base;
using hou_elearning.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace hou_elearning.DataAccess
{    
    public class BaivietDAL : SqlProvider<BaivietEntity>
    {
        static BaivietDAL()
        {
            InitReader();
        }
        protected static void InitReader()
        {
            getFromReader=delegate(SqlDataReader dr)
            {
                BaivietEntity entity = new BaivietEntity();
				entity.PK_iBaivietID = Int64.Parse("0"+dr["PK_iBaivietID"].ToString());
				entity.FK_iNguoivietID = Int64.Parse("0"+dr["FK_iNguoivietID"].ToString());
				entity.sNoidung = dr["sNoidung"].ToString();
                entity.sTenbaiviet = dr["sTenbaiviet"].ToString();
                entity.sTieude = dr["sTieude"].ToString();
                entity.sGhichu = dr["sGhichu"].ToString();
				entity.FK_iTainguyenID = Int64.Parse("0"+dr["FK_iTainguyenID"].ToString());
				entity.dNgaydang =String.IsNullOrEmpty(dr["dNgaydang"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaydang"].ToString());
				entity.iTrangthai = Int16.Parse("0"+dr["iTrangthai"].ToString());
				entity.FK_iNguoicapnhat = Int64.Parse("0"+dr["FK_iNguoicapnhat"].ToString());
				entity.dNgaycapnhat =String.IsNullOrEmpty(dr["dNgaycapnhat"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaycapnhat"].ToString());
                return entity;
            };
        }
        public static BaivietEntity GetOne(Int64 PK_iBaivietID)
        {
            string cmdName = "spBaiviet_GetByPK";
            SqlParameter p = new SqlParameter("@PK_iBaivietID", PK_iBaivietID);
            BaivietEntity entity = GetOne(cmdName, p);
            return entity;
        }
        public static List<BaivietEntity> GetAll()
        {
            string cmdName = "spBaiviet_Get";
            return GetList(cmdName);
        }
        /**
         * Hàm lấy số bài viết mới nhất
         * truyền vào số bài viết cần lấy ra
         **/
        public static List<BaivietEntity> GetTop()
        {
            string cmdName = "[spBaiviet_GetTop]";
            List<BaivietEntity> list = GetList(cmdName);
            return list;
        }
        public static List<BaivietEntity> GetByFK_iNguoivietID(Int64 FK_iNguoivietID)
		{
			string cmdName = "spBaiviet_GetByFK_FK_iNguoivietID";
			SqlParameter p = new SqlParameter("@FK_iNguoivietID",FK_iNguoivietID);
			List<BaivietEntity> list = GetList(cmdName, p);
			return list;
		}
        public static List<BaivietEntity> GetByFK_iTainguyenID(Int64 FK_iTainguyenID)
		{
			string cmdName = "spBaiviet_GetByFK_FK_iTainguyenID";
			SqlParameter p = new SqlParameter("@FK_iTainguyenID",FK_iTainguyenID);
			List<BaivietEntity> list = GetList(cmdName, p);
			return list;
		}public static List<BaivietEntity> GetByFK_iNguoicapnhat(Int64 FK_iNguoicapnhat)
		{
			string cmdName = "spBaiviet_GetByFK_FK_iNguoicapnhat";
			SqlParameter p = new SqlParameter("@FK_iNguoicapnhat",FK_iNguoicapnhat);
			List<BaivietEntity> list = GetList(cmdName, p);
			return list;
		}
        public static int Add(BaivietEntity entity)
        {
            string cmdName = "spBaiviet_Insert";
            return Run(cmdName,true,initParams(entity));

        }
        public static bool Edit(BaivietEntity entity)
        {
            string cmdName = "spBaiviet_Update";
            return Run(cmdName,false,initParams(entity))>0;
        }
        public static bool Remove(Int64 PK_iBaivietID)
        {
            string cmdName = "spBaiviet_Delete";
            SqlParameter p = new SqlParameter("@PK_iBaivietID", PK_iBaivietID);
            return Run(cmdName,false,p)>0;
        }
        #region private
        private static SqlParameter[] initParams(BaivietEntity entity)
        {
            SqlParameter[] p = new SqlParameter[8];
			p[0] = new SqlParameter("@PK_iBaivietID", entity.PK_iBaivietID);
			p[1] = new SqlParameter("@FK_iNguoivietID", entity.FK_iNguoivietID);
			p[2] = new SqlParameter("@sNoidung", entity.sNoidung);
			p[3] = new SqlParameter("@FK_iTainguyenID", entity.FK_iTainguyenID);
			p[4] = new SqlParameter("@dNgaydang", entity.dNgaydang);
			p[5] = new SqlParameter("@iTrangthai", entity.iTrangthai);
			p[6] = new SqlParameter("@FK_iNguoicapnhat", entity.FK_iNguoicapnhat);
			p[7] = new SqlParameter("@dNgaycapnhat", entity.dNgaycapnhat);
            return p;
        }
        #endregion
       
    }
}